package com.my.study.java;

import com.my.study.common.constant.TopicConstants;
import com.my.study.java.interceptors.ProducerPrefixInterceptor;
import com.my.study.java.interceptors.ProducerPrefixInterceptorPlus;
import com.my.study.java.partiton.DemoPartitioner;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 生产者
 *
 * @author: yidujun
 * @create: 2021/07/23 10:51
 */
public class ProducerTest {

    private static final String BROKER_LIST = "47.98.50.37:9092";

    private static Properties initConfig() {
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, BROKER_LIST);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        // KafkaProducer在客户端对应的id
        props.put(ProducerConfig.CLIENT_ID_CONFIG, "producer.id.demo");
        // 失败重试次数
//        props.put(ProducerConfig.RETRIES_CONFIG, 5);

        props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, ProducerPrefixInterceptor.class.getName() + ","+ ProducerPrefixInterceptorPlus.class.getName());
        return props;
    }

    public static void main(String[] args) {
        Properties prop = initConfig();
        KafkaProducer<String, String> producer = new KafkaProducer<>(prop);

        String msg = "Hello Kafka";
        ProducerRecord<String, String> producerRecord = new ProducerRecord<>(TopicConstants.TOPIC02, msg);

        try {
            // 发送即忘
            sendForForget(producer, producerRecord);
            // 同步
//            sync(producer, producerRecord);
            // 异步
//            async(producer, producerRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            producer.close();
        }
    }

    /**
     * 发后即忘
     *
     * @param producer
     * @param record
     */
    private static void sendForForget(KafkaProducer<String, String> producer, ProducerRecord<String, String> record) {
        producer.send(record);
    }

    /**
     * 同步
     *
     * @param producer
     * @param record
     */
    private static void sync(KafkaProducer<String, String> producer, ProducerRecord<String, String> record) {
        try {
            Future<RecordMetadata> send = producer.send(record);
            RecordMetadata metadata = send.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    /**
     * 异步
     *
     * @param producer
     * @param record
     */
    private static void async(KafkaProducer<String, String> producer, ProducerRecord<String, String> record) {
        producer.send(record, ((metadata, exception) -> {
            // 发生失败
            if (null != exception) {
                // 可以做一些日志记录
                exception.printStackTrace();
            }
            // 发生成功
            else {
                System.out.println(metadata.toString());
            }
        }));
    }
}
